<html>
  <head>
    <title>TransferLocalStorage</title>
  </head>
  <body>
    <script type="text/javascript">
      /* $$CookieReader taken from angular.js source:
          https://github.com/angular/angular.js/blob/f768da24884504cb1a683f8429e55c64a2397478/src/ng/cookieReader.js */
      function $$CookieReader(rawDocument) {
        var lastCookies = {};
        var lastCookieString = '';

        function safeGetCookie(rawDocument) {
          try {
            return rawDocument.cookie || '';
          } catch (e) {
            return '';
          }
        }

        function safeDecodeURIComponent(str) {
          try {
            return decodeURIComponent(str);
          } catch (e) {
            return str;
          }
        }

        return function() {
          var cookieArray, cookie, i, index, name;
          var currentCookieString = safeGetCookie(rawDocument);

          if (currentCookieString !== lastCookieString) {
            lastCookieString = currentCookieString;
            cookieArray = lastCookieString.split('; ');
            lastCookies = {};

            for (i = 0; i < cookieArray.length; i++) {
              cookie = cookieArray[i];
              index = cookie.indexOf('=');
              if (index > 0) { //ignore nameless cookies
                name = safeDecodeURIComponent(cookie.substring(0, index));
                // the first value that is seen for a cookie is the most
                // specific one.  values for the same cookie name that
                // follow are for less specific paths.
                if (lastCookies[name] === undefined) {
                  lastCookies[name] = safeDecodeURIComponent(cookie.substring(index + 1));
                }
              }
            }
          }
          return lastCookies;
        };
      }

      var responseToJson = function (response) {
        return response.json()
      }

      fetch('/Resources/wallet-options.json').then(responseToJson).then(function (options) {
        var targetOrigin = options.domains.comWalletApp
        var cookieReader = {}

        var postMessage = function (type, payload) {
          var message = { type: type, payload: payload }
          window.parent.postMessage(message, targetOrigin)
        }

        var deliverItem = function (id, data) {
          postMessage('cookie', { id: id, data: data })
        }

        var finishedDelivering = function () {
          postMessage('cookies-sent', null)
        }

        var readAndSendItem = function (id, sendId) {
          sendId = sendId || id
          // wallet prefixes localStorage values with "ls."
          var lsValue = localStorage.getItem('ls.' + id)
          if (lsValue != null) {
            deliverItem(sendId, lsValue)
          } else {
            // try to find old value in ng cookie if not in localStorage
            var cookieValue = cookieReader[id]
            if (cookieValue != null) {
              deliverItem(sendId, cookieValue)
            }
          }
        }

        if (window.parent !== window) {
          cookieReader = $$CookieReader(document)()

          var items = [
            { id: 'guid' },
            { id: 'uid', as: 'guid' },
            { id: 'alert-success' },
            { id: 'alert-warning' },
            { id: 'backup-reminder' },
            { id: 'bcash-about' },
            { id: 'buy-alert-seen' },
            { id: 'buy-bitcoin-reminder' },
            { id: 'chart' },
            { id: 'chart-data' },
            { id: 'contextual-message' },
            { id: 'dismissedRecurringBuyIntro' },
            { id: 'ethereum-survey' },
            { id: 'hasSeenBchAddressChangeNotice' },
            { id: 'hasSeenSfoxBuyIntro' },
            { id: 'hasSeenSfoxSellIntro' },
            { id: 'hasSignedUpForSfoxBuyAccess' },
            { id: 'hideBitcoinCashWelcome' },
            { id: 'hideEtherWelcome' },
            { id: 'logout-survey' },
            { id: 'logout-survey-1515433556478' },
            { id: 'session' },
            { id: 'sfox-buy-survey' },
            { id: 'sfox-has-seen' },
            { id: 'sfox-has-seen-buy' },
            { id: 'sfox-survey' },
            { id: 'shift-trade-survey' },
            { id: 'showMobileConversion' },
            { id: 'subscribed' },
            { id: 'theme' },
            { id: 'whatsNewViewed' }
          ]

          items.forEach(function (item) {
            readAndSendItem(item.id, item.as)
          })

          setTimeout(function () {
            finishedDelivering()
          })
        }
      })
    </script>
  </body>
</html>
